﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;

namespace PeekWebApp.handler
{
    /// <summary>
    /// RegUserLogin 的摘要说明
    /// </summary>
    public class RegUserLogin : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string username =context.Server.HtmlDecode(context.Request["username"].Trim());
            string pwd = context.Server.HtmlDecode(context.Request["password"].Trim());
            UserRegistry user = new UserRegistryService().GetAll().Find(o =>(o.UserName.Equals(username) && o.PassWord.Equals(pwd)&&o.IsEmailVerification&&o.State==1)
                || (o.Email.Equals(username) && o.PassWord.Equals(pwd) && o.IsEmailVerification && o.State == 1));
            if (user != null)
            {
              
                HttpCookie cookie = new HttpCookie("user", user.UserName);             
                context.Response.SetCookie(cookie);
                try
                {
                    context.Response.Write(context.Request.UrlReferrer.ToString());//返回上次请求的地址
                }
                catch {
                    context.Response.Write(context.Request.RawUrl.ToString());//返回当前请求的地址
                }
                
            }
            else
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}